package com.atguigu.school.controller;

import com.atguigu.school.commons.Page;
import com.atguigu.school.commons.Result;
import com.atguigu.school.pojo.Admin;
import com.atguigu.school.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    /**
     * 处理管理员相关业务
     */
    @Autowired
    private AdminService adminService;

    @PostMapping("/saveOrUpdateAdmin")
    @ApiOperation(value = "新增或更新管理员信息", notes = "如果无 id 就是新增，有 id 就是更新")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin) {
        this.adminService.saveOrUpdateAdmin(admin);
        return Result.ok();
    }

    @DeleteMapping("/deleteAdmin")
    @ApiOperation(value = "批量删除管理员")
    @ApiImplicitParam(name = "ids", value = "批量删除管理员")
    public Result deleteAdmin(@RequestBody List<Integer> ids) {
        this.adminService.deleteAdminsByIds(ids);
        return Result.ok();
    }

    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询管理员")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页，从 1 开始"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度"),
            @ApiImplicitParam(name = "adminName", value = "管理员名称，可以不完整", required = false)
    })
    public Result getAllAdmin(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, String adminName) {
        Page<Admin> page = this.adminService.selectAdminsByName(adminName, pageNo, pageSize);
        return Result.ok(page);
    }

}
